package states;

import model.Assets;
import model.GUI;
import model.Stage;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import controller.Global;

public class Play extends BasicGameState {
	private int id;

	public Play(int id) {
		this.id = id;
	}

	@Override
	public int getID() {
		return id;
	}

	@Override
	public void render(GameContainer container, StateBasedGame game, Graphics g)
			throws SlickException {
		Global.stage.render(g);
		Global.hud.render(g);
		Global.gui.render(g);
	}

	@Override
	public void update(GameContainer container, StateBasedGame game, int millis)
			throws SlickException {

		Global.stage.update();
		Global.gui.processInput(game);

		if (Global.gui.isProgramStopped()) {
			container.exit();
		}
	}

	@Override
	public void mouseWheelMoved(int direction) {
		Global.gui.mouseWheelMoved(direction);
	}

	@Override
	public void mouseMoved(int x, int y, int x1, int y1) {
		Global.gui.mouseMoved(x, y, x1, y1);
	}

	@Override
	public void mousePressed(int button, int x, int y) {
		Global.gui.mousePressed(button, x, y);
	}

	@Override
	public void keyPressed(int code, char c) {
		Global.gui.keyPressed(code, c);
	}

	@Override
	public void keyReleased(int code, char c) {
		Global.gui.keyReleased(code, c);
	}

	@Override
	public void enter(GameContainer container, StateBasedGame game)
			throws SlickException {
		super.enter(container, game);
	}

	@Override
	public void leave(GameContainer container, StateBasedGame game)
			throws SlickException {
		super.leave(container, game);
	}

	@Override
	public void init(GameContainer container, StateBasedGame game)
			throws SlickException {

		// load game assets
		Assets.getInstance().loadAssets();

		// create stage (TODO be able to dynamically load stages from XML)
		new Stage(Stage.SIDE, Assets.getInstance().getImage(
				"assets/gfx/background.png"), false);

		// get input handler
		Global.input = container.getInput();
		Global.gui = new GUI();
	}
}
